<?
session_start();
include('conn.php');
$opt = $_REQUEST["q"];
	switch($opt)
	{
		case '0':
			$qry1 = "SELECT * from replacePair where status = '1' order by id desc;";
			$qry = mysql_query($qry1);
			$data = array();
			while($rows = mysql_fetch_array($qry))
			{
				$q1 = "select stbno,cardno from cardTable c,stbTable s where c.id = (select cardid from STBDetails where pairid='".$rows['oldpair']."') and s.id= (select stbid from STBDetails where pairid='".$rows['oldpair']."')";
				$r1 = mysql_fetch_array(mysql_query($q1));
				$q2 = "select stbno,cardno from cardTable c,stbTable s where c.id = (select cardid from STBDetails where pairid='".$rows['newpair']."') and s.id= (select stbid from STBDetails where pairid='".$rows['newpair']."')";
				$r2 = mysql_fetch_array(mysql_query($q2));				
				$data[] = array
					(			
						"id" => $rows['id'],			
						"oldstb" => $r1['stbno'],
						"oldcard" => $r1['cardno'],
						"newstb" => $r2['stbno'],
						"newcard" => $r2['cardno'],
						"rdate" => $rows['replacedate'],
						"reason" => $rows['reason'],
						"comments" => $rows['comments']			
					);
			}
			echo json_encode($data);
			break;
			
		case '1':
			$newstbno = $_REQUEST['stbno'];			
			$qry1 = "SELECT cardno from cardTable where status = '0' and  id in (select cardid from STBDetails where stbid in (select id from stbTable where stbno='$newstbno')); ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))			
				$data = $rows['cardno'];								
			echo ($data);												
			break;
							
		
		case '3':
			$qry1 = "select pairid from STBDetails where Active='1' and pairid in (select pairid from STBtoCustomer where TerminationDate is null);";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry)){
				$q2 = "select s.stbno,c.cardno from stbTable s,cardTable c where s.id = (select stbid from STBDetails where pairid='".$rows['pairid']."') and c.id =(select cardid from STBDetails where pairid='".$rows['pairid']."') ";
				$r2 = mysql_fetch_array(mysql_query($q2));
				$data[] = array
					(			
						"s" => $r2['stbno'],
						"c" => $r2['cardno'],						
					);
			}
			echo json_encode($data);
			break;				
			
		case '5':
			$qry1 = "select pairid from STBDetails where Active='1' and pairid not in (select pairid from STBtoCustomer where TerminationDate is null);";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry)){
				$q2 = "select s.stbno,c.cardno from stbTable s,cardTable c where s.id = (select stbid from STBDetails where pairid='".$rows['pairid']."') and c.id =(select cardid from STBDetails where pairid='".$rows['pairid']."') ";
				$r2 = mysql_fetch_array(mysql_query($q2));
				$data[] = array
					(			
						"s" => $r2['stbno'],
						"c" => $r2['cardno'],						
					);
			}
			echo json_encode($data);
			break;
			
	
	}
		
?>